//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.hustcad.plm.pdm.flowbusiness.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.hustcad.plm.basic.log.LogEventEnum;
import com.hustcad.plm.basic.log.TyplmLogger;
import com.hustcad.plm.basic.util.TyplmEntityBaseUtil;
import com.hustcad.plm.pdm.common.util.CommonUtil;
import com.hustcad.plm.pdm.flowbusiness.model.entity.FlowObject;
import com.hustcad.plm.pdm.flowbusiness.model.entity.FlowObjectDetailInfo;
import com.hustcad.plm.pdm.flowbusiness.model.entity.WorkFlowObjectPackage;
import com.hustcad.plm.pdm.flowbusiness.model.vo.ProcessStartWithNotObjListVO;
import com.hustcad.plm.pdm.flowbusiness.service.TyplmFlowObjectPackageService;
import com.hustcad.plm.pdm.flowbusiness.service.TyplmWorkFlowObjectPackageService;
import com.hustcad.plm.pdm.workflow.model.entity.MarkInfo;
import com.hustcad.plm.pdm.workflow.model.entity.WorkFlowContainerObject;
import com.hustcad.plm.pdm.workflow.model.vo.CreateWorkFlowContainerObjectVO;
import com.hustcad.plm.pdm.workflow.service.TyplmWorkFlowProcessService;
import com.hustcad.plm.pdm.workflow.service.errormark.TyplmCommonErrorMarkService;
import com.ty.basic.entity.CTyEntityBaseDO;
import com.ty.basic.entity.audit.AuditLogDO;
import com.ty.basic.handler.exceptionhandler.exception.ResponseCodeEnum;
import com.ty.basic.handler.exceptionhandler.exception.TyException;
import com.ty.basic.response.ResponseResult;
import com.ty.basic.util.TySpringBeanUtil;
import com.ty.basic.utils.ArgumentUtils;
import com.ty.basic.utils.TyResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api(
        tags = {"flowObjectPackage"}
)
@RequestMapping({"/rest/v1/flowObjectPackage"})
public class TyplmFlowObjectPackageController {
    @Resource
    private TyplmFlowObjectPackageService typlmFlowObjectPackageService;
    @Resource
    private TyplmWorkFlowObjectPackageService typlmWorkFlowObjectPackageService;
    @Resource
    private TyplmWorkFlowProcessService typlmWorkFlowProcessService;
    @Resource
    private TyplmLogger typlmLogger;

    public TyplmFlowObjectPackageController() {
    }

    @PostMapping({"/setErrorMark"})
    @ApiOperation(
            value = "设置错误标记",
            notes = "设置错误标记",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult<Boolean> setErrorMark(@RequestBody MarkInfo markInfo) {
        BigInteger linkOid = markInfo.getLinkOid();
        ArgumentUtils.checkArgumentBlank(linkOid, "相关对象");
        String taskDefKey = markInfo.getTaskDefKey();
        ArgumentUtils.checkArgumentBlank(taskDefKey, "任务定义");
        TyplmCommonErrorMarkService commonErrorMarkService = this.getServiceBeanByLinkOtype(markInfo.getLinkOtype());
        commonErrorMarkService.setErrorMark(linkOid, taskDefKey);
        return ResponseResult.success();
    }

    @PostMapping({"/updateObjectLinkDistributeObjects"})
    @ApiOperation(
            value = "分配数据",
            notes = "分配数据",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult<Boolean> updateObjectLinkDistributeObjects(@RequestBody WorkFlowObjectPackage workFlowObjectPackage) {
        BigInteger baseObjectOid = workFlowObjectPackage.getBaseObjectOid();
        String baseObjectOtype = workFlowObjectPackage.getBaseObjectOtype();
        ArgumentUtils.checkArgumentBlank(baseObjectOid, "流程主对象OID");
        ArgumentUtils.checkArgumentBlank(baseObjectOtype, "流程主对象OTYPE");
        this.typlmWorkFlowObjectPackageService.updateObjectLinkDistributeObjects(workFlowObjectPackage);
        return ResponseResult.success();
    }

    @PostMapping({"/setConfirmMark"})
    @ApiOperation(
            value = "设置已确认",
            notes = "设置已确认",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult<Boolean> setConfirmMark(@RequestBody MarkInfo markInfo) {
        BigInteger linkOid = markInfo.getLinkOid();
        ArgumentUtils.checkArgumentBlank(linkOid, "相关对象");
        String taskDefKey = markInfo.getTaskDefKey();
        ArgumentUtils.checkArgumentBlank(taskDefKey, "任务定义");
        TyplmCommonErrorMarkService commonErrorMarkService = this.getServiceBeanByLinkOtype(markInfo.getLinkOtype());
        commonErrorMarkService.setConfirmMark(linkOid, taskDefKey);
        return ResponseResult.success();
    }

    @PostMapping({"/removeErrorMark"})
    @ApiOperation(
            value = "取消错误标记",
            notes = "取消错误标记",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult<Boolean> removeErrorMark(@RequestBody MarkInfo markInfo) {
        BigInteger linkOid = markInfo.getLinkOid();
        ArgumentUtils.checkArgumentBlank(linkOid, "相关对象");
        String taskDefKey = markInfo.getTaskDefKey();
        ArgumentUtils.checkArgumentBlank(taskDefKey, "任务定义");
        TyplmCommonErrorMarkService commonErrorMarkService = this.getServiceBeanByLinkOtype(markInfo.getLinkOtype());
        commonErrorMarkService.removeErrorMark(linkOid, taskDefKey);
        return ResponseResult.success();
    }

    @PostMapping({"/setModifiedMark"})
    @ApiOperation(
            value = "设置已修改标记",
            notes = "设置已修改标记",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult<Boolean> setModifiedMark(@RequestBody MarkInfo markInfo) {
        BigInteger linkOid = markInfo.getLinkOid();
        ArgumentUtils.checkArgumentBlank(linkOid, "相关对象");
        TyplmCommonErrorMarkService commonErrorMarkService = this.getServiceBeanByLinkOtype(markInfo.getLinkOtype());
        commonErrorMarkService.setModifiedMark(linkOid);
        return ResponseResult.success();
    }

    private TyplmCommonErrorMarkService getServiceBeanByLinkOtype(String linkOtype) {
        if (StringUtils.isBlank(linkOtype)) {
            linkOtype = StringUtils.defaultString("com.ty.entity.workFlow.FlowObjectPackageLink");
        }

        if ("com.ty.entity.workFlow.FlowObjectPackageLink".equals(linkOtype)) {
            return (TyplmCommonErrorMarkService)TySpringBeanUtil.getBean("typlmFlowObjectPackageLinkServiceImpl");
        } else if ("ty.inteplm.change.CTyChangeActRecordLink".equals(linkOtype)) {
            return (TyplmCommonErrorMarkService)TySpringBeanUtil.getBean("typlmChgActRecordLinkExtServiceImpl");
        } else {
            List<String> linkOtypeList = Arrays.asList("com.ty.entity.workFlow.FlowObjectPackageLink", "ty.inteplm.change.CTyChangeActRecordLink");
            throw new TyException(Collections.singletonList(new TyException(ResponseCodeEnum.ACC_PARAMETER_ENUM_NOT_CONTAINERS_ERROR, new String[]{"linkOtype", linkOtype, StringUtils.join(linkOtypeList, ",")})));
        }
    }

    @PostMapping({"/batchSetErrorMark"})
    @ApiOperation(
            value = "批量设置错误标记",
            notes = "批量设置错误标记",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult<Boolean> batchSetErrorMark(@RequestBody List<MarkInfo> markInfoList) {
        this.checkLinkData(markInfoList);
        TyplmCommonErrorMarkService commonErrorMarkService = this.getServiceBeanByLinkOtype(((MarkInfo)markInfoList.get(0)).getLinkOtype());
        commonErrorMarkService.batchSetErrorMark(markInfoList);
        return ResponseResult.success();
    }

    @PostMapping({"/batchSetConfirmMark"})
    @ApiOperation(
            value = "批量设置已确认",
            notes = "批量设置已确认",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult<Boolean> batchSetConfirmMark(@RequestBody List<MarkInfo> markInfoList) {
        this.checkLinkData(markInfoList);
        TyplmCommonErrorMarkService commonErrorMarkService = this.getServiceBeanByLinkOtype(((MarkInfo)markInfoList.get(0)).getLinkOtype());
        commonErrorMarkService.batchSetConfirmMark(markInfoList);
        return ResponseResult.success();
    }

    @PostMapping({"/batchRemoveErrorMark"})
    @ApiOperation(
            value = "批量取消错误标记",
            notes = "批量取消错误标记",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult<Boolean> batchRemoveErrorMark(@RequestBody List<MarkInfo> markInfoList) {
        this.checkLinkData(markInfoList);
        TyplmCommonErrorMarkService commonErrorMarkService = this.getServiceBeanByLinkOtype(((MarkInfo)markInfoList.get(0)).getLinkOtype());
        commonErrorMarkService.batchRemoveErrorMark(markInfoList);
        return ResponseResult.success();
    }

    @PostMapping({"/batchSetModifiedMark"})
    @ApiOperation(
            value = "批量设置已修改标记",
            notes = "批量设置已修改标记",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult<Boolean> batchSetModifiedMark(@RequestBody List<MarkInfo> markInfoList) {
        this.checkLinkData(markInfoList);
        TyplmCommonErrorMarkService commonErrorMarkService = this.getServiceBeanByLinkOtype(((MarkInfo)markInfoList.get(0)).getLinkOtype());
        commonErrorMarkService.batchSetModifiedMark(markInfoList);
        return ResponseResult.success();
    }

    private void checkLinkData(List<MarkInfo> markInfoList) {
        TyResultUtils.requireNonNull(markInfoList, "markInfoList");
        if (markInfoList.stream().anyMatch((item) -> {
            return TyplmEntityBaseUtil.isEmptyOid(item.getLinkOid());
        })) {
            TyException.throwSingleTyExceptionVarargs(ResponseCodeEnum.ACC_PARAMETER_IS_NULL, new String[]{"linkOid"});
        }

    }

    @PostMapping({"/checkWorkFlowStartRulesCommon"})
    @ApiOperation(
            value = "整合新建流程包的结构化传参",
            notes = "整合新建流程包的结构化传参",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult<WorkFlowObjectPackage> checkWorkFlowStartRulesCommon(@RequestBody WorkFlowContainerObject workFlowContainerObject) {
        ArgumentUtils.checkArgumentNull(workFlowContainerObject, "workFlowContainerObject");
        WorkFlowObjectPackage workFlowObjectPackage=this.typlmFlowObjectPackageService.checkWorkFlowStartRulesCommon(workFlowContainerObject);
        workFlowObjectPackage.setWorkFlowId(null);
        workFlowObjectPackage.setUserTaskList(new ArrayList<>());
        return ResponseResult.success(workFlowObjectPackage);
    }

    @PostMapping({"/checkWorkFlowStartRulesCommonWithNotObjList"})
    @ApiOperation(
            value = "无对象集合新建流程包",
            notes = "无对象集合新建流程包",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult<WorkFlowObjectPackage> checkWorkFlowStartRuesCommonWithNotObjList(@RequestBody CreateWorkFlowContainerObjectVO createWorkFlowContainerObjectVO) {
        ArgumentUtils.checkArgumentNull(createWorkFlowContainerObjectVO, "createWorkFlowContainerObjectVO");
        return ResponseResult.success(this.typlmFlowObjectPackageService.checkWorkFlowStartRuesCommonWithNotObjList(createWorkFlowContainerObjectVO));
    }

    @Transactional(
            rollbackFor = {Exception.class}
    )
    @PostMapping({"/newAndStartWorkFlowObjectPackageWithNotObjList"})
    @ApiOperation(
            value = "无对象集合新建对象包流程并启动",
            notes = "无对象集合新建对象包流程并启动",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult<Boolean> newAndStartWorkFlowObjectPackageWithNotObjList(@RequestBody ProcessStartWithNotObjListVO processStartWithNotObjListVO) {
        try {
            this.typlmWorkFlowObjectPackageService.insertWithNotObjList(processStartWithNotObjListVO.getWorkFlowObjectPackage(), processStartWithNotObjListVO.getUserIdList());
            this.addAuditLog(processStartWithNotObjListVO.getWorkFlowObjectPackage(), LogEventEnum.CRATE_OBJECT);
            this.typlmWorkFlowProcessService.startProcess(processStartWithNotObjListVO.getWorkFlowObjectPackage(), true);
            this.addAuditLog(processStartWithNotObjListVO.getWorkFlowObjectPackage());
            return ResponseResult.success(true);
        } catch (Exception var3) {
            this.addErrorAuditLog(processStartWithNotObjListVO.getWorkFlowObjectPackage(), var3);
            throw var3;
        }
    }

    private void addAuditLog(WorkFlowObjectPackage workFlowObjectPackage, LogEventEnum logEventEnum) {
        if (workFlowObjectPackage != null) {
            this.typlmLogger.info(new CTyEntityBaseDO(workFlowObjectPackage.getOid(), workFlowObjectPackage.getOtype()), logEventEnum);
        }

    }

    private void addAuditLog(WorkFlowObjectPackage workFlowObjectPackage) {
        if (workFlowObjectPackage != null && workFlowObjectPackage.getFlowObjectList() != null) {
            Iterator var2 = workFlowObjectPackage.getFlowObjectList().iterator();

            while(var2.hasNext()) {
                FlowObject flowObject = (FlowObject)var2.next();
                this.typlmLogger.info(new CTyEntityBaseDO(flowObject.getRefObjectOid(), flowObject.getRefObjectOtype()), LogEventEnum.ADD_TO_WORKFLOW);
            }
        }

    }

    private void addErrorAuditLog(WorkFlowObjectPackage workFlowObjectPackage, Exception exception) {
        AuditLogDO auditLogDO = new AuditLogDO();
        auditLogDO.setLifecyclestatekey(workFlowObjectPackage.getLifeCycleStage());
        auditLogDO.setObjoid(BigInteger.ZERO);
        auditLogDO.setObjotype("com.ty.entity.workFlow.WorkFlowContainer");
        auditLogDO.setTargetname(workFlowObjectPackage.getName());
        auditLogDO.setContaineroid(workFlowObjectPackage.getContainerOid());
        auditLogDO.setContainerotype(workFlowObjectPackage.getContainerOtype());
        this.typlmLogger.errorInfo(LogEventEnum.CRATE_OBJECT, auditLogDO, exception);
    }

    @Transactional(
            rollbackFor = {Exception.class}
    )
    @PostMapping({"/checkWorkFlowStartRulesAndStartWorkFlowObjectPackageWithNotObjList"})
    @ApiOperation(
            value = "测试无对象创建流程包并启动流程",
            notes = "测试无对象创建流程包并启动流程",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult<Boolean> checkWorkFlowStartRulesAndStartWorkFlowObjectPackageWithNotObjList(@RequestBody CreateWorkFlowContainerObjectVO createWorkFlowContainerObjectVO) {
        ArgumentUtils.checkArgumentNull(createWorkFlowContainerObjectVO, "createWorkFlowContainerObjectVO");
        WorkFlowObjectPackage workFlowObjectPackage = this.typlmFlowObjectPackageService.checkWorkFlowStartRuesCommonWithNotObjList(createWorkFlowContainerObjectVO);
        List<BigInteger> userIdList = new ArrayList();
        userIdList.add(CommonUtil.getUser().getOid());
        this.typlmWorkFlowObjectPackageService.insertWithNotObjList(workFlowObjectPackage, userIdList);
        this.typlmWorkFlowProcessService.startProcess(workFlowObjectPackage, true);
        return ResponseResult.success(true);
    }

    /** @deprecated */
    @Deprecated
    @PostMapping({"/queryObjectDetailInfo"})
    @ApiOperation(
            value = "查询对象使用详细信息",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult<PageInfo<FlowObjectDetailInfo>> queryObjectDetailInfo(@RequestBody JSONObject params) {
        PageInfo<FlowObjectDetailInfo> flowObjectDetailInfoList = this.typlmFlowObjectPackageService.queryObjectDetailInfo(params);
        return ResponseResult.success(flowObjectDetailInfoList);
    }
}
